import time
from generator import DatasetGenerator
from alg import SortingAlgorithms, insertion_sort

def is_sorted(arr):
    return all(arr[i] <= arr[i+1] for i in range(len(arr)-1))

def test_algorithm(name, func, data):
    start = time.perf_counter()
    result = func(data)
    end = time.perf_counter()
    correct = is_sorted(result)
    return correct, end - start

def main():
    generator = DatasetGenerator()
    algs = SortingAlgorithms()
    alg_list = [
        ("冒泡排序", algs.bubble_sort),
        ("插入排序", insertion_sort),
        ("快速排序", algs.quick_sort),
        ("堆排序", algs.heap_sort),
        ("归并排序", algs.merge_sort),
        ("希尔排序", algs.shell_sort)
    ]
    sizes = [100, 1000, 5000]
    types = [
        ("随机", generator.generate),
        ("已排序", generator.generate_sorted),
        ("逆序", generator.generate_reversed),
        ("部分有序", generator.generate_partially_sorted)
    ]
    for size in sizes:
        for tname, tfunc in types:
            print(f"\n数据类型: {tname}, 大小: {size}")
            data = tfunc(size)
            for alg_name, alg_func in alg_list:
                correct, elapsed = test_algorithm(alg_name, alg_func, data)
                print(f"{alg_name:<8} 正确: {correct}  时间: {elapsed:.6f}秒")

if __name__ == "__main__":
    main()
